import 'dart:async';
import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/entity/user_center/bank_card_item_entity.dart';
import 'package:cbec_app/entity/user_center/khh_list_entity.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

// 银行卡控制器
class BankController extends BaseController {
  ///银行卡列表
  RxList<BankCardItemEntity?>? cardList = <BankCardItemEntity>[].obs;
  var cardType = 1.obs;

  ///验证码base64
  var captchaImg = ''.obs;

  ///验证码key
  String captchaKey = '';

  ///开户行索引
  RxInt khhIndex = 0.obs;

  ///开户行名称
  var kkhName = ''.obs;

  var countdown = 0.obs; //倒计时数字
  var countdownTxt = '获取验证码'.obs; //倒计时的文本

  ///开户行icon
  var kkhIcon = ''.obs;

  ///开户行id
  RxInt kkhId = 0.obs;

  ///当前页码
  var page = 1.obs;

  ///开户行列表
  RxList<KhhListEntity?> khhlist = <KhhListEntity>[].obs;

  ///姓名
  TextEditingController nameTxtCtr = TextEditingController();

  ///银行卡号
  TextEditingController bankCardTxtCtr = TextEditingController();

  ///身份证号
  TextEditingController idcardTxtCtr = TextEditingController();

  ///手机号
  TextEditingController phoneTxtCtr = TextEditingController();

  ///验证码
  TextEditingController vertifyTxtCtr = TextEditingController();

  ///手机号验证码
  TextEditingController phonevertifyTxtCtr = TextEditingController();

  @override
  void onInit() {
    super.onInit();
    getBankList();
  }

  ///获取银行列表数据
  Future getBankList() async {
    Tools.showLoading();
    return await Network.getBankCardList().then((value) {
      logger.d('银行卡 ==== ${value}');
      cardList?.value = value!;      
      logger.d('银行卡2 ==== ${cardList?.value}');
      Tools.hideLoading();
    }).catchError((error){
      print(error);
      Tools.hideLoading();
    });
  }

  ///新增初始化
  void initAdd() {
    getImageCodes();
    getKhhList();
    bankCardTxtCtr.text = '';
    vertifyTxtCtr.text = '';
    captchaKey = '';
    idcardTxtCtr.text = '';
    phoneTxtCtr.text = '';
    phonevertifyTxtCtr.text = '';
    nameTxtCtr.text = '';
    Get.toNamed(Routes.addBankCard);
  }

  void getImageCodes() async {
    Tools.showLoading();
    await Network.getImageCode().then((value) {
      captchaImg.value = value?['captchaImg'];
      captchaKey = value?['captchaKey'];
      Tools.hideLoading();
    });
  }

  ///获取开户行列表
  void getKhhList() {
    Network.getKhhList().then((value) {
      khhlist.value = value!;
      if (value.isNotEmpty) {
        kkhId.value = value[0]!.id;
        kkhName.value = value[0]!.name;
      }
    });
  }

  Timer? timer;

  ///获取验证码
  void startCountdown() {
    if(phoneTxtCtr.text==''){
      Tools.showToast("请先填写预留手机号");
      return;
    }
    if (countdown.value == 0) {
      Network.getMsgCode(phoneTxtCtr.text,"add_bank").then((value) {
        if (value == true) {
          countdown.value = 60;
          timer = Timer.periodic(Duration(seconds: 1), (timer) {
            countdown.value--;
            countdownTxt.value = "(${countdown.value.toString()}s)";
            if (countdown.value == 0) {
              countdownTxt.value = '重新发送';
              timer.cancel();
            }
          });
        }
      });
    }
  }

  ///选择开户行
  void choiseKhh(int index) {
    khhIndex.value = index;
    kkhId.value = khhlist[index]!.id;
    kkhName.value = khhlist[index]!.name;
    kkhIcon.value = khhlist[index]!.icon!;
  }

  ///提交
  void submit() {
    Network.addBankCard(
            bankCardTxtCtr.text,
            kkhId.value,
            kkhName.value,
            vertifyTxtCtr.text,
            captchaKey,
            idcardTxtCtr.text,
            phoneTxtCtr.text,
            phonevertifyTxtCtr.text,
            nameTxtCtr.text)
        .then((value) {
      if (value == true) {
        Get.back();
        getBankList();
      }
    });
  }

  ///删除银行卡
  void delete(String id,String tag){
    Network.deleteBankcard(id).then((value){
      if(value==true){
        Tools.showToast("删除成功");
        getBankList();
        SmartDialog.dismiss(tag: tag);
      }
    });
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    await getBankList();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    super.onPageLoading();
    page.value += 1;
    await getBankList();
    baseRefreshCtr.loadComplete();
  }

  @override
  void onClose() {
    if (timer != null) {
      timer!.cancel();
    }
    super.onClose();
  }
}
